<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      padding: 0;
      margin: 0;
    }
    .box {
      width: 200px;
      height: 200px;
      background: red;
      position: absolute;
      left: 0;
      top: 0;
    }
    .box h4 {
      background: #ccc;
    }
  </style>
</head>
<body>
  <div id="box1" class="box">box</div>
  <div id="box2" class="box">
    <h4>title</h4>
  </div>
  <script>
    class Drag {
      constructor (selector, title) {
        this.box = document.querySelector(selector)
        // 如果参数传了title，那么就从box里面找到title，否则还是box本身
        this.title = title ? this.box.querySelector(title) : this.box
        this.bindEvents()
      }
      bindEvents () {
        this.title.onmousedown = e => {
          e = e || window.event
          // 这里记录的是鼠标到事件源的坐标，这个值在整个拖拽过程中是固定的
          let disX = e.offsetX
          let disY = e.offsetY
          document.onmousemove = e => {
            e = e || window.event
            let left = e.clientX - disX
            let top = e.clientY - disY
            this.move(left, top)
          }
          document.onmouseup = function () {
            this.onmousemove = null
          }
          // 防止全选文字造成的bug
          if (e.preventDefault) {
            e.preventDefault()
          } else {
            return false
          }
        }
      }
      move (left, top) {
        // 判断范围
        if (left < 0) left = 0
        if (top < 0) top = 0
        // todo：右下角
        this.box.style.left = left + 'px'
        this.box.style.top = top + 'px'
      }
    }

    new Drag('#box1')
    new Drag('#box2', 'h4')

  </script>
</body>
</html>